﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.Object;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess
{
    public class UserDAO
    {
        private DataAccess _conn;
        public UserDAO()
        {
            _conn = new DataAccess();
            
        }

        public int Login(users us)
        {
            SqlParameter[] param = new SqlParameter[2];

            param[0] = new SqlParameter("@P_TAI_KHOAN", SqlDbType.NVarChar);
            param[0].Value = us.username;

            param[1] = new SqlParameter("@P_MAT_KHAU", SqlDbType.NVarChar);
            param[1].Value = us.password;

            return _conn.ExecuteScalar("LOGIN", param);
        }

        public void DOI_MAT_KHAU(string tai_khoan,string mat_khau_moi)
        {
            SqlParameter[] param = new SqlParameter[2];

            param[0] = new SqlParameter("@P_TAI_KHOAN", SqlDbType.NVarChar);
            param[0].Value = tai_khoan;

            param[1] = new SqlParameter("@P_MAT_KHAU_MOI", SqlDbType.NVarChar);
            param[1].Value = mat_khau_moi;

            _conn.ExecuteNonQuery("UPDATE_MAT_KHAU", param);
        }

        public DataTable DS_NHAN_VIEN()
        {
            return _conn.ExecuteDataTable("DS_NHAN_VIEN");
        }

        public int KT_NGUOIDUNG(string taikhoan)
        {
            SqlParameter[] param = new SqlParameter[1];

            param[0] = new SqlParameter("@P_TAI_KHOAN", SqlDbType.NVarChar);
            param[0].Value = taikhoan;

            return _conn.ExecuteScalar("KT_NGUOIDUNG", param);
        }

        public void THEM_NHAN_VIEN(users us)
        {
            SqlParameter[] param = new SqlParameter[6];

            param[0] = new SqlParameter("@P_TAI_KHOAN", SqlDbType.NVarChar);
            param[0].Value = us.username;

            param[1] = new SqlParameter("@P_MAT_KHAU", SqlDbType.NVarChar);
            param[1].Value = us.password;

            param[2] = new SqlParameter("@P_QUYEN_HAN", SqlDbType.NVarChar);
            param[2].Value = us.role;

            param[3] = new SqlParameter("@P_NGAY_SINH", SqlDbType.DateTime);
            if (us.birthday != "")
                param[3].Value = us.birthday;
            else
                param[3].Value = DBNull.Value;

            param[4] = new SqlParameter("@P_SDT", SqlDbType.NVarChar);
            param[4].Value = us.phone_number;

            param[5] = new SqlParameter("@P_NGAY_DK", SqlDbType.DateTime);
            param[5].Value = us.reg_date;

            _conn.ExecuteNonQuery("THEM_NHAN_VIEN", param);
        }

        public void SUA_NHAN_VIEN(users us)
        {
            SqlParameter[] param = new SqlParameter[7];

            param[0] = new SqlParameter("@P_ID", SqlDbType.Int);
            param[0].Value = us.users_id;

            param[1] = new SqlParameter("@P_TAI_KHOAN", SqlDbType.NVarChar);
            param[1].Value = us.username;

            param[2] = new SqlParameter("@P_MAT_KHAU", SqlDbType.NVarChar);
            param[2].Value = us.password;

            param[3] = new SqlParameter("@P_QUYEN_HAN", SqlDbType.NVarChar);
            param[3].Value = us.role;

            param[4] = new SqlParameter("@P_NGAY_SINH", SqlDbType.DateTime);
            if (us.birthday != "")
                param[4].Value = us.birthday;
            else
                param[4].Value = DBNull.Value;

            param[5] = new SqlParameter("@P_SDT", SqlDbType.NVarChar);
            param[5].Value = us.phone_number;

            param[6] = new SqlParameter("@P_NGAY_DK", SqlDbType.DateTime);
            if (us.reg_date != "")
                param[6].Value = us.reg_date;
            else
                param[6].Value = DBNull.Value;

            _conn.ExecuteNonQuery("SUA_NHAN_VIEN", param);
        }

        public DataTable KQ_NHAN_VIEN(int user_id)
        {
            SqlParameter[] param = new SqlParameter[1];

            param[0] = new SqlParameter("@P_NV_ID", SqlDbType.Int);
            param[0].Value = user_id;

            return _conn.ExecuteDataTable("KQ_NHAN_VIEN", param);
        }

        public DataTable KQ_NHAN_VIEN_BY_TAI_KHOAN(string taikhoan_nv)
        {
            SqlParameter[] param = new SqlParameter[1];

            param[0] = new SqlParameter("@P_TAI_KHOAN", SqlDbType.VarChar);
            param[0].Value = taikhoan_nv;

            return _conn.ExecuteDataTable("KQ_NHAN_VIEN_BY_TAI_KHOAN", param);
        }

        public void XOA_NHAN_VIEN(int user_id)
        {
            SqlParameter[] param = new SqlParameter[1];

            param[0] = new SqlParameter("@P_ID", SqlDbType.Int);
            param[0].Value = user_id;

            _conn.ExecuteNonQuery("XOA_NHAN_VIEN", param);
        }
    }
}
